home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / argv.zip / ARGV.TXT
Text File  |  1987-01-25  |  5KB  |  153 lines

  1.  
  2.                                                  Page 1 of 3
  3.  
  4.                        A R G C  &  A R G V
  5.                    The Command Line Variables
  6.  
  7.                                By
  8.  
  9.                           S. R. Sampson
  10.                       Compuserve 75136,626
  11.  
  12. Recently  there was a discussion concerning pointers in  'C'  and
  13. the question was asked:
  14.  
  15.      Why do some programs use **argv and others use *argv[]?
  16.  
  17. This  article will explain the meanings of these two declarations
  18. and show why they can be considered synonamous.
  19.  
  20. Wherever  you see **argv,  you can substitute *argv[].   They are
  21. both the same:
  22.  
  23.         char   **argv;     In English, argv is a pointer to a
  24.                             pointer to a character.
  25.  
  26.         char   *argv[];    In English, argv is an array of
  27.                             pointers to characters.
  28.  
  29. Initially  we have what appears to be two different  declarations
  30. both visually and when written in English.   In the first example
  31. it is implying that there is only one pointer to a character.  In
  32. the  second  example  it is implying that there are one  or  many
  33. pointers to characters; or an array of pointers.
  34.  
  35. Let's  go  through  an example and see how the  command  line  is
  36. operated on and stored in memory.
  37.  
  38. For  instance let's say an operator typed:
  39.  
  40.         cc -O test.c
  41.  
  42. Which would look like this in memory:
  43.  
  44.        _____________________________________________________
  45.       /___________________________________________________/|
  46.       | c | c |  | - | O |  | t | e | s | t | . | c | \n |/
  47.       ----------------------------------------------------
  48.  
  49. When  you  execute  a 'C' compiled program the first  thing  that
  50. executes  is  the  run-time library which is  attached  to  every
  51. program.   This  will examine the command line and search  for  a
  52. space delimiter between options or a newline character to signify
  53. the end of the command.    The run-time will gather  each  option
  54. into  character arrays (strings).   For instance our example will
  55. produce the following strings:
  56.  
  57.         "cc", "-O", "test.c"
  58.  
  59.  
  60.                                                  Page 2 of 3
  61.  
  62.  
  63. A R G C  &  A R G V
  64.  - The Command Line Variables....
  65.  
  66.  
  67. These strings are stored in memory and will appear like so:
  68.  
  69.     _________________________________________________________
  70.    /_____________/____________/____________________________/|
  71.    | c | c | \0 | - | O | \0 | t | e | s | t | . | c | \0 |/
  72.    --------------------------------------------------------
  73.      ^            ^            ^
  74.      |            |            |
  75.    0900         0903         0906
  76.  
  77.  
  78. The run-time also makes an array of pointers which will point  to
  79. the  start  of  each string.   These pointers will be  stored  in
  80. memory also.   Let's call the array argv.   In our sample command
  81. line  we  have three string arrays,  so we will also  have  three
  82. pointers.  The examples are using imaginary memory locations  for
  83. storage.
  84.  
  85.           argv[0] = 0x0900  which points to "cc"
  86.           argv[1] = 0x0903  which points to "-O"
  87.           argv[2] = 0x0906  which points to "test.c"
  88.  
  89. and will look like this in memory:
  90.  
  91.  
  92.              argv[0]     argv[1]   argv[2]
  93.            ________________________________
  94.           /__________/_________/_________/|
  95.           | 09 | 00 | 09 | 03 | 09 | 06 |/
  96.           -------------------------------
  97.              ^         ^         ^
  98.              |         |         |
  99.            0950      0952      0954
  100.  
  101.  
  102. The memory pointer 0x0900 is a pointer to a character.  Well,  we
  103. have  to  store this pointer somewhere,  Let's put it  in  memory
  104. location 0x0950,  as illustrated above,  and put the next pointer
  105. in location 0x0952 and finally the third in 0x0954.
  106.  
  107.  
  108.  
  109.                                                  Page 3 of 3
  110.  
  111. A R G C  &  A R G V
  112.  - Command Line Variables....
  113.  
  114.  
  115. Question:  What does memory location 0x0950 have in it?
  116.  
  117.   Answer:  It has 0x0900 - a pointer to a character.
  118.  
  119.  
  120. So 0x0950 is a pointer to - a pointer to a character:
  121.  
  122.           char  **argv;
  123.  
  124. You can see now how the declaration has meaning, and you can also
  125. see  that  it does not fully describe the true state  of  things.
  126. That  is,  there may be more pointers than just one.  Many people
  127. like   the array declaration which tends to  describe   the  true
  128. structure of most command lines:
  129.  
  130.           char    *argv[];
  131.  
  132. The  operation  of the run-time package finally finishes  and  is
  133. ready  to begin your program.   It calls your program just like a
  134. function call:
  135.  
  136.           main( 3,0950 )
  137.  
  138. Which  says  we have three pointers beginning at  location  0950.
  139. Then along comes your program:
  140.  
  141. main( argc, argv )
  142. int     argc;        argc is the number of pointers (3).
  143. char    **argv;      argv is a pointer (0950) to a pointer (0900)
  144.                       to a character 'c'.
  145.      or
  146.  
  147. char    *argv[];     argv is an array  of  pointers to characters
  148.                       beginning at (0950).
  149.  
  150. I  hope  this  will assist you in visualizing  the  command  line
  151. process and answer an often asked question.
  152.  
  153.